﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

//https://go.microsoft.com/fwlink/?LinkId=234236 上介绍了“用户控件”项模板

namespace Acoris.UWP.Demo
{
    using Windows.UI;

    using Microsoft.Graphics.Canvas;
    using Microsoft.Graphics.Canvas.Brushes;
    using Microsoft.Graphics.Canvas.UI;
    using Microsoft.Graphics.Canvas.UI.Xaml;


    internal abstract class TriggerBase
    {

    }

    internal sealed class EventTrigger:TriggerBase
    {

    }

    enum State
    {
        NoState,
        Hover,
        Pressed
    }

    [Demo(typeof(VisualState),Name ="可视化状态测试")]
    public sealed partial class VisualState : UserControl
    {
        private EventTrigger eventTrigger;
        private State state;
        private Rect rect;

        public VisualState()
        {
            this.InitializeComponent();
            eventTrigger = new EventTrigger();
        }

        private void canvas_CreateResources(CanvasControl sender,CanvasCreateResourcesEventArgs args)
        {
            rect = new Rect(200, 200, 200, 200);
        }

        private void canvas_Draw(CanvasControl sender,CanvasDrawEventArgs args)
        {
            var session = args.DrawingSession;
            switch (state)
            {
                case State.Pressed:
                    session.FillRectangle(rect, Colors.DarkBlue);
                    break;
                case State.Hover:
                    session.FillRectangle(rect, Colors.Blue);
                    break;
                case State.NoState:
                default:
                    session.FillRectangle(rect, Colors.LightBlue);
                    break;
            }
        }

        private void canvas_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            if (EvaluatedCollide(e.GetCurrentPoint(canvas).Position))
            {
                state = State.Hover;
            }
            else
            {
                state = State.NoState;
            }
        }

        private void canvas_PointerExited(object sender, PointerRoutedEventArgs e)
        {
            if (!EvaluatedCollide(e.GetCurrentPoint(canvas).Position))
            {
                state = State.NoState;
            }
        }

        private void canvas_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            if (EvaluatedCollide(e.GetCurrentPoint(canvas).Position))
            {
                state = State.Pressed;
            }
            else
            {
                state = State.NoState;
            }
        }

        private bool EvaluatedCollide(Point point)
        {
            var result = point.X >= rect.X && point.X <= rect.X + rect.Width &&
                   point.Y >= rect.Y && point.Y <= rect.Y + rect.Height;
            canvas.Invalidate();
            return result;
        }
    }
}
